using System;
using MonoTouch.UIKit;
using MonoTouch.iAd;
using Microsoft.Xna.Framework;
using AdRotator.Model;

using System.Drawing;

namespace AdRotatorXNA
{     
    internal class iAdComponent : AdComponent
    {
        private static UIViewController _rootViewController;
        private static iAdComponent _instance;
        private static ADBannerView _currentAd = null;
        private static bool _initialized = false;
        private static float _contentScale = 1.0f;
        
        private static AdMobViewDelegate _bannerDelegate;
        
        private iAdComponent (Game game)
            : base (game) { }
        
        public static iAdComponent Current
        {
            get
            {
                if (AdRotatorXNAComponent.game == null)
                    return null;
                
                if (_instance == null)
                    _instance = new iAdComponent(AdRotatorXNAComponent.game);
                        
                return _instance;
            }
        }
        
        public static void Initialize(Game _game, string applicationID)
        {
            if (_initialized)
                return;
            
            _rootViewController = (UIViewController)AdRotatorXNAComponent.game.Services.GetService(typeof(UIViewController));
            _contentScale = 1.0f / _rootViewController.View.ContentScaleFactor;
            
            _initialized = true;
        }
        
        public override void UpdateAd(AdInformation adInfo)
        {
            if (_currentAd != null)
                return;
            
            State = AdState.Initialized;
            
            AdInfo = adInfo;
            
            _currentAd = new ADBannerView(new RectangleF(0, 0, adInfo.Width * _contentScale, adInfo.Height * _contentScale));
            _currentAd.AdLoaded += AdLoaded;
            _currentAd.FailedToReceiveAd += AdLoadFailed;
            
            //_currentAd.Delegate = _bannerDelegate;
            _rootViewController.View.AddSubview(_currentAd);
            
            _currentAd.BecomeFirstResponder();
            
            _currentAd.Hidden = true;
        }
        
        private void AdLoaded(object sender, EventArgs args)
        {
            if (_currentAd != null)
                _currentAd.Hidden = false;
            
            State = AdState.AdLoaded;
        }
        
        private void AdLoadFailed(object sender, EventArgs args)
        {
            if (_currentAd != null)
                _currentAd.Hidden = true;
            
            State = AdState.AdFailed;
        }
        
        protected override void OnEnabledChanged (object sender, EventArgs args)
        {
            if (!Enabled)
                RemoveCurrentControl();
            
            base.OnEnabledChanged (sender, args);
        }
        
        protected override void UnloadContent ()
        {
            RemoveCurrentControl();
            base.UnloadContent ();
        }
        
        private void RemoveCurrentControl()
        {
            if (_currentAd == null)
                return;
            
            State = AdState.NotInitialized;
            
            _currentAd.Hidden = true;
            
            _currentAd.Delegate = null;
            
            _currentAd.AdLoaded -= AdLoaded;
            _currentAd.FailedToReceiveAd -= AdLoadFailed;
           
            _currentAd.RemoveFromSuperview();
            
            _currentAd = null;
        }
        
        public override void UpdatePosition(Vector2 newPos)
        {
            if (_currentAd == null)
                return;
            
            newPos*= _contentScale;
            
            // It looks like we're getting rotated portrait ads that are scaled up
            // if on a retina device.
            _currentAd.Frame = new RectangleF( newPos.X, newPos.Y, AdInfo.Width * _contentScale, AdInfo.Height * _contentScale);
        }
    }
}

